package com.pie.face_pie.controller;

import com.pie.face_pie.service.DeadlineService;
import com.pie.face_pie.service.DetectService;
import com.pie.face_pie.service.RateService;
import com.pie.face_pie.tool.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.*;
import java.io.IOException;
import java.sql.SQLException;

@RestController
@RequestMapping("/face")
public class FaceController {
    @Autowired
    private RateService rateService;

    @Autowired
    private DetectService detectService;

    @Autowired
    private DeadlineService deadlineService;

    @PostMapping("/register")
    public Result register(@RequestParam("image") MultipartFile[] multipartFiles, @RequestParam("id") String id) throws IOException, SQLException {
        //判断是否在截至日期
        int res = deadlineService.checkDeadline(id);
        if(res == 0) {
            return Result.fail("已过截至日期");
        }

        int result = rateService.rate(multipartFiles, id);
        if (result == 1) {
            return Result.success();
        }
        return Result.fail("请对准后再尝试");
    }

    @PostMapping("/login")
    public Result login(@RequestParam("image") MultipartFile multipartFile) throws IOException, SQLException {
        int res = detectService.detect(multipartFile);
        if(res == 0) {
            return Result.fail("签到失败");
        } else {
            return Result.success("签到成功",null);
        }
    }
}
